package org.bigk.invoices.utils;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.struts2.ServletActionContext;
import org.displaytag.util.ParamEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DisplayTagUtils {

	private static final Logger logger = LoggerFactory.getLogger(DisplayTagUtils.class);

	private DisplayTagUtils() {
	}

	public static int getCurrentPage4Id(String idParam) {
		if (logger.isDebugEnabled()) {
			logger.debug("getCurrentPage4Id(String idParam=" + idParam + ") - start");
		}
		
		ParamEncoder encoder = new ParamEncoder(idParam);
		String encodedParam = encoder.encodeParameterName("p");
		if (logger.isDebugEnabled()) {
			logger.debug("getCurrentPage4Id(String) - encodedParam=" + encodedParam);
		}
		
		HttpServletRequest request = ServletActionContext.getRequest();
		String currentPageStr = request.getParameter(encodedParam);
		if (logger.isDebugEnabled()) {
			logger.debug("getCurrentPage4Id(String) - currentPageStr=" + currentPageStr);
		}
		
		int currentPageInt = NumberUtils.toInt(currentPageStr, 1);
		if (logger.isDebugEnabled()) {
			logger.debug("getCurrentPage4Id(String) - end - return value=" + currentPageInt);
		}
		return currentPageInt;
	}
}
